package com.mvc.demo.web;

/**
 * Created by 峥桂 on 2017/9/8.
 */

import com.mvc.demo.dao.entity.usert.UserT;
import com.mvc.demo.service.usert.UserTService;
import com.tools.hry.xls.AnalysisXlsTool;
import com.tools.hry.xls.CreateXlsTool;
import com.tools.hry.xls.analysis.AnalysisResult;
import com.tools.hry.xls.analysis.ICallbackFunction;
import com.tools.hry.xls.create.CreateResult;
import com.tools.hry.xls.create.IWriteCallbackFunction;
import com.tools.hry.xls.create.ParamBean;
import com.tools.hry.xls.exception.ExcelErrorLogBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserTService userService;

    @RequestMapping("/showUser")
    public ModelAndView toIndex(HttpServletRequest request) {
        int userId = Integer.parseInt(request.getParameter("id"));
        UserT user = this.userService.selectByPrimaryKey(userId);
        ModelAndView modelAndView = new ModelAndView("showUser");
        modelAndView.addObject("user", user);
        return modelAndView;
    }

    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public ModelAndView upload(@RequestParam("bin") MultipartFile multipartFile) {
        ModelAndView modelAndView = new ModelAndView("showUploadResult");
        final Response rsp=new Response();
        rsp.setSuccess(true);
        modelAndView.addObject("responseData", rsp);

        try {
            AnalysisXlsTool.readInputStreamAndToEntity(multipartFile.getInputStream(),
                    ClaimsExcelBeanRead.class,0,0,new ICallbackFunction<ClaimsExcelBeanRead>() {
                        public void onFailure(AnalysisResult excelUtil) throws Exception {
                            System.out.println("===onFailure=====");
                            System.out.println(excelUtil.getErrorLogBeans());
                            System.out.println(excelUtil.getException().getMessage());
                            rsp.setData_err(excelUtil.getErrorLogBeans());
                            //  if(e!=null){
                            //  throw e;
                            //  }
                        }
                        public void onSuccess(AnalysisResult excelUtil, List<ClaimsExcelBeanRead> list_) throws Exception {
                            System.out.println("=====onSuccess===");
                            System.out.println(excelUtil.getErrorLogBeans());
                            rsp.setData_err(excelUtil.getErrorLogBeans());
                            rsp.setData(list_);
                            for (ClaimsExcelBeanRead bean:list_) {
                                System.out.println(bean.toString());
                            }
                        }
                        public void before(AnalysisResult excelUtil) throws Exception {
                        }



                        public void after(AnalysisResult result) throws Exception {

                        }
                    });
        } catch (Exception e) {
        }
        return modelAndView;
    }

    @RequestMapping(value = "down")
    public void down(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            String fileNm =System.currentTimeMillis()+"活动数据.xls";
            response.setHeader("Content-Disposition", "filename=" + new String(fileNm.getBytes("UTF8"), "iso8859-1"));//设置文件头编码方式和文件名

            ParamBean paramBean=new ParamBean();
            paramBean.setActionName("dsfsf");
            paramBean.setTitleName("活动数据");
            paramBean.setCreateTime(new Date().toLocaleString());
            paramBean.setSelfTemplatePath("d://template.xls");
            paramBean.setDatalist(XlsService.getList());

            boolean flag=CreateXlsTool.createXLS(paramBean,fileNm, response.getOutputStream(),
                    new IWriteCallbackFunction() {
                        public void onFailure(CreateResult result) {

                        }

                        public void onSuccess(CreateResult result) {

                        }
                    });
            System.out.println(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }










































}
